/*
 * @Author: didididijiajiajia 11643763+didididijiajiajia@user.noreply.gitee.com
 * @Date: 2024-03-25 09:46:49
 * @LastEditors: didididijiajiajia 11643763+didididijiajiajia@user.noreply.gitee.com
 * @LastEditTime: 2024-04-05 16:30:07
 * @FilePath: \react全栈\vue_ts-server\app.js
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
// 导入 express 模块
const express = require('express')
// 创建 express 的服务器实例
const app = express()
const http = require('http');
const server = http.createServer(app);  
const WebSocket = require('ws');

const wss = new WebSocket.Server({ server });  

// 处理 WebSocket 连接
wss.on('connection', function connection(ws) {
  ws.on('message', function incoming(message) {
    console.log('received: %s', message);
  });

  ws.send('Hello! Message From Server!!');
});

// HTTP 服务器上的 '/websocket' 路由
app.get('/websocket', (req, res) => {
  res.send('WebSocket route');
});


// 导入 cors 中间件
const cors = require('cors')
// 将 cors 注册为全局中间件
app.use(cors())
// 导入验证规则中间件
const joi = require('joi')

const bodyParser = require('body-parser')
app.use(
  bodyParser.urlencoded({
    extended: true
  })
)
app.use(bodyParser.json())
// 开启静态资源的访问
app.use('/public/avatar', express.static('./public/avatar'))
// 导入配置文件
const config = require('./config/index')
// 解析 token 的中间件
const expressJWT = require('express-jwt')
// 使用 .unless({ path: [/^\/api\//] }) 指定哪些接口不需要进行 Token 的身份认证
app.use(
  expressJWT({ secret: config.jwtSecretKey }).unless({
    path: ['/user/login', '/user/checkCode', '/user/refreshToken', '/user/addUser','/article/all','/websocket']
  })
)

// 导入并注册用户路由模块
const userRouter = require('./router/user')
app.use('/user', userRouter)
// 导入并注册用户角色模块
const roleRouter = require('./router/role')
app.use('/user/role', roleRouter)
// 导入并注册用户菜单模块
const menuRouter = require('./router/menu')
app.use('/user/menu', menuRouter)
// 导入用户信息路由模块
const userinfoRouter = require('./router/userinfo')
app.use('/user/myInfo', userinfoRouter)
// 导入字典路由模块
const dictRouter = require('./router/dict')
app.use('/dict', dictRouter)
// 导入字典项路由模块
const dictItemRouter = require('./router/dict-items')
app.use('/dict/item', dictItemRouter)
//导入文章路由模块
const articleRouter = require('./router/article')
app.use('/article', articleRouter)
// 定义错误级别的中间件
app.use((err, req, res, next) => {
  // 数据验证失败
  if (err instanceof joi.ValidationError) return res.send({ code: 1, message: err.message })
  // token解析失败
  if (err.name === 'UnauthorizedError') return res.send({ code: 401, message: '身份认证失败' })
  // 未知错误
  return res.send({ code: 500, message: err.message })
})
// 调用 app.listen 方法，指定端口号并启动web服务器
server.listen(9999, function () {
  console.log('api server running')
})
console.log(server);
//启动websocket
server.on('upgrade', function upgrade(request, socket, head) {  
  console.log("111");
  // 检查请求的路径是否为 '/websocket'  
  if (request.url === '/websocket') {  
    wss.handleUpgrade(request, socket, head, function done(ws) {  
      wss.emit('connection', ws, request);  
    });  
  } else {  
    socket.destroy();  
  }  
});